﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;


public partial class Education_TermSetting : ContainerBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //获取当前学期学年设置
    [System.Web.Services.WebMethod]
    public static TermInfo GetGloblTermInfo()
    {
        IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
        
        List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();
        if (globalTermInfoList.Count == 0)
        {
            return null;
        }
        else
        {
            IList<TermInfo> terminfo = Container<ITermInfoLogic>.Instance.FindTermInfo(SchoolMark, globalTermInfoList[0].GlobalYear, globalTermInfoList[0].GlobalTerm);
            //return globalTermInfoList[0];
            if (terminfo == null || terminfo.Count < 1)
                return null;
            return terminfo[0];
        }
    }

    //设置当前学年学期信息
    [System.Web.Services.WebMethod]
    public static int UpdateGloblTermInfo(GlobalTermInfo globalTermInfo, String startDate, String endDate)
    {
        int result = 0;
        IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
        List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();

        if (globalTermInfoList == null || globalTermInfoList.Count < 1 || globalTermInfoList[0].RID == 0)
        {
            //如果没有设置过学年学期信息，则新增一条设置
            result = globalTermInfoLogic.Create(globalTermInfo);
        }
        else
        {
            //如果已经设置过学年学期信息，则修改原来记录
            globalTermInfo.RID = globalTermInfoList[0].RID;
            result = globalTermInfoLogic.Update(globalTermInfo);
        }

        if (result > 0)
        {
            TermInfo ti = new TermInfo();


            DateTime dts = DateTime.MinValue;
            DateTime dte = DateTime.MinValue;
            if (!DateTime.TryParse(startDate, out dts))
                return -1;
            ti.StartDate = dts;
            if (DateTime.TryParse(endDate, out dte))
                ti.EndDate = dte;

            ti.SchoolMark = SchoolMark;
            ti.SchoolYear = globalTermInfo.GlobalYear;
            ti.Term = globalTermInfo.GlobalTerm;
            
            Container<ITermInfoLogic>.Instance.Create(ti);
        }

        if (result > 0 && Common.CurrentVersion == EduVersion.School)
        {
            

            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", Common.GetConstSchoolMark());
            //查询学校信息表，获取该校学期值
            ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
            List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)schoolInfoLogic.FindAll(ht);
            if ((listSchoolInfo == null) || (listSchoolInfo.Count == 0))
                return 0;
            List<int> temp = new List<int>();
            switch (listSchoolInfo[0].SchoolOpt)
            {
                case 1:
                    temp.Add(1);
                    temp.Add(0);
                    temp.Add(0);
                    break;
                case 2:
                    temp.Add(0);
                    temp.Add(1);
                    temp.Add(0);
                    break;
                case 3:
                    temp.Add(0);
                    temp.Add(0);
                    temp.Add(1);
                    break;
                case 4:
                    temp.Add(1);
                    temp.Add(1);
                    temp.Add(0);
                    break;
                case 5:
                    temp.Add(0);
                    temp.Add(1);
                    temp.Add(1);
                    break;
                case 6:
                    temp.Add(1);
                    temp.Add(1);
                    temp.Add(1);
                    break;
            }
            TermUpgrade(temp);
            ht = new Hashtable();
            ht.Add("SchoolYear", globalTermInfo.GlobalYear);
            ht.Add("Term", globalTermInfo.GlobalTerm);
            Container<IStudentHistoryLogic>.Instance.InsertNewStudentHistory(ht);
        }
        return result;
    }

    /// <summary>
    /// 学期升级，这里未做升级学期失败后的处理
    /// </summary>
    /// <returns></returns>
    public static int TermUpgrade(List<int> deptList)
    {
        IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());

        //查询学校信息表，获取该校学期值
        List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)schoolInfoLogic.FindAll(ht);
        if ((listSchoolInfo == null) || (listSchoolInfo.Count == 0))
            return 0;

        //查询局级学年学期表，获取全局学期值
        List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();
        if ((globalTermInfoList == null) || (globalTermInfoList.Count == 0))
            return 0;

        //如果学校学期与全局学期不一致
        if ((listSchoolInfo[0].SchoolYear != globalTermInfoList[0].GlobalYear) ||
             (listSchoolInfo[0].SchoolTerm != (byte)globalTermInfoList[0].GlobalTerm))
        {
            //如果全局学期为上学期，则需要学校学年升级
            if (globalTermInfoList[0].GlobalTerm == 1)
            {
                //调用学年升级处理过程
                YearUpgrade(deptList, Common.GetConstSchoolMark());
            }
            else
            { 
                // 学期升级权限表升级
                Container<ITeacherRolesLogic>.Instance.UpdateTerm(Common.GetConstSchoolMark(), globalTermInfoList[0].GlobalYear);
            }

            //学校同步学期设置信息
            listSchoolInfo[0].SchoolYear = globalTermInfoList[0].GlobalYear;
            listSchoolInfo[0].SchoolTerm = (byte)globalTermInfoList[0].GlobalTerm;
            schoolInfoLogic.Update(listSchoolInfo[0]);

            //升级成功
            return 1;
        }

        //无需升级
        return 2;
    }
    /// <summary>
    /// 学校学年升级
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <returns></returns>
    public static int YearUpgrade(List<int> deptList, string schoolMark)
    {
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", schoolMark);
        //选择部分学部升级
        ht.Add("GSCHOOL", deptList[0]);
        ht.Add("JHSCHOOL", deptList[1]);
        ht.Add("SHSCHOOL", deptList[2]);
        schoolInfoLogic.YearUpgrade(ht);

        return 1;
    }
}
